package com.pay.cloud.fragment;

import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.pay.cloud.IconFontFactory;
import com.pay.cloud.KeyInfo;
import com.pay.cloud.R;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.AUDIO_SERVICE;
import static android.media.AudioManager.ADJUST_LOWER;
import static android.media.AudioManager.FLAG_PLAY_SOUND;
import static android.media.AudioManager.STREAM_MUSIC;
import static android.media.AudioManager.STREAM_SYSTEM;
import static com.pay.cloud.KeyInfo.KEY_DOWN;
import static com.pay.cloud.KeyInfo.KEY_NUM_0;
import static com.pay.cloud.KeyInfo.KEY_NUM_1;
import static com.pay.cloud.KeyInfo.KEY_NUM_2;
import static com.pay.cloud.KeyInfo.KEY_NUM_3;
import static com.pay.cloud.KeyInfo.KEY_NUM_4;
import static com.pay.cloud.KeyInfo.KEY_NUM_5;
import static com.pay.cloud.KeyInfo.KEY_NUM_6;
import static com.pay.cloud.KeyInfo.KEY_NUM_7;
import static com.pay.cloud.KeyInfo.KEY_NUM_8;
import static com.pay.cloud.KeyInfo.KEY_NUM_9;
import static com.pay.cloud.KeyInfo.KEY_UP;


public class SoundFragment extends Fragment {

    private static final List<KeyInfo> validKeys = new ArrayList<>();

    static {
        validKeys.add(KEY_UP);
        validKeys.add(KEY_DOWN);
        validKeys.add(KEY_NUM_0);
        validKeys.add(KEY_NUM_1);
        validKeys.add(KEY_NUM_2);
        validKeys.add(KEY_NUM_3);
        validKeys.add(KEY_NUM_4);
        validKeys.add(KEY_NUM_5);
        validKeys.add(KEY_NUM_6);
        validKeys.add(KEY_NUM_7);
        validKeys.add(KEY_NUM_8);
        validKeys.add(KEY_NUM_9);
    }

    private AudioManager audioManager;
    private View view;
    private MediaPlayer mediaPlayer;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_sound, container, false);

        Typeface typeface = IconFontFactory.getHalflings(getContext());
        TextView downView = view.findViewById(R.id.btn_sound_down);
        downView.setTypeface(typeface);
        TextView upView = view.findViewById(R.id.btn_sound_up);
        upView.setTypeface(typeface);

        audioManager = (AudioManager) getContext().getSystemService(AUDIO_SERVICE);

        mediaPlayer = MediaPlayer.create(getContext(), R.raw.qrcode);
        mediaPlayer.setLooping(false);

        TextView textView = view.findViewById(R.id.text_sound_value);
        textView.setText("" + audioManager.getStreamVolume(STREAM_MUSIC));

        return view;

    }

    public void onKeyDown(KeyInfo keyInfo) {
        if (!validKeys.contains(keyInfo)) {
            return;
        }
        if (keyInfo == KEY_UP) {
            audioManager.adjustStreamVolume(STREAM_MUSIC, AudioManager.ADJUST_RAISE, FLAG_PLAY_SOUND);
            audioManager.adjustStreamVolume(STREAM_SYSTEM, AudioManager.ADJUST_RAISE,0);
        } else if (keyInfo == KeyInfo.KEY_DOWN) {
            audioManager.adjustStreamVolume(STREAM_MUSIC, ADJUST_LOWER, FLAG_PLAY_SOUND);
            audioManager.adjustStreamVolume(STREAM_SYSTEM, ADJUST_LOWER,0);
        } else {
            audioManager.setStreamVolume(STREAM_MUSIC, Integer.parseInt(keyInfo.getValue()), FLAG_PLAY_SOUND);
            audioManager.setStreamVolume(STREAM_SYSTEM, Integer.parseInt(keyInfo.getValue()), 0);
        }
        TextView textView = view.findViewById(R.id.text_sound_value);
        textView.setText("" + audioManager.getStreamVolume(STREAM_MUSIC));
        mediaPlayer.start();
    }

}
